﻿#include "Sensor.h"
#include "Box.h"
#include "ShareComponents.h"
#include "Camera.h"
#include "Box.h"
#include "Sprite.h"
#include "MainGame.h"
#include "GunWeapon.h"
#include "RSManager.h"
#include <d3dx9.h>

#define ATTACK_RANK 250

Sensor::Sensor(Box* box, BULLET_ID gunWeapon_id): Object(box,Obj_STATIC)
{
	_gunWeapon_id =gunWeapon_id;
	
}
Sensor::~Sensor(void)
{

}
void Sensor::init()
{
	_sprite= RSManager::getInstance()->getSprite(SENSOR);
	_isAlive=true;
	_hits=1;
	delay=0;	
	_colBox->_v.x = 0.0f;
	_colBox->_v.y = 0.0f;
}
void Sensor::update()
{

	//tinh khoang cach giữa 2 đối tượng player và sensor		
	bool ck1 = _box->getRight() < gl_camera->getRightSide();
	bool ck2 = _box->getLeft() < gl_camera->getLeftSide() - 10;
	if(ck2)
	{
		_sprite->Reset(0);
		return;
	}
	if(!ck1) return;
	delay+=GAME_TIME;
	if(delay > 300)
	{
		delay=0;
		_sprite->Next();
	}
	if(_hits < 0)
	{
		GunWeapon* gunWeapon =new GunWeapon
			(new Box(_box->_position,D3DXVECTOR2(48,30),D3DXVECTOR2(3,0)),_gunWeapon_id);
		gunWeapon->init();
		gl_MainGame->addWorldObject(gunWeapon);
		_isAlive=false;
	}


}

void Sensor::render()
{	
	_sprite->Render(_box->_position);
	Object::render();
}

Box* Sensor::getCollisionBox()
{
	if(_sprite->GetCurrentIndex() != 2) return gl_BOX_0_0_0;
	_colBox->_position=_box->_position;
	float delta = 10;
	_colBox->_size.x = _box->_size.x - delta;
	_colBox->_size.y = _box->_size.y - delta;
	_colBox->_position.x += delta /2.0f;
	_colBox->_position.y += delta /2.0f;	
	return _colBox;
}
